package com.itheima.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
//@DefaultProperties(defaultFallback = "fallback")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    //@HystrixCommand
    @RequestMapping(value = "/getAll/user/list")
    public String getAll(){
//        String url = "http://localhost:18081/user/findAll";\
        //获取实例列表
//        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("provider");
//        //实例信息
//        ServiceInstance serviceInstance = serviceInstances.get(0);

        String url = "http://provider/usre/find/all/1/1";
        String forObject = restTemplate.getForObject(url, String.class);
        //String forObject = restTemplate.postForObject(url,new Object(),String.class);

        String url2 = "http://providerpro/usre/find/all/asdf/1/1";

        String forObject2 = restTemplate.getForObject(url2, String.class);
        System.out.println(forObject);
        return forObject+ forObject2;
    }


    /**
     * 新建一个入口 api  等同于consumer 在配置........可以访问
     * @return
     */
    //@HystrixCommand
    @RequestMapping(value = "/getAll/user/list/login")
    public String getAllLogin(){
//        String url = "http://localhost:18081/user/findAll";\
        //获取实例列表
//        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("provider");
//        //实例信息
//        ServiceInstance serviceInstance = serviceInstances.get(0);

        String url = "http://providerpro/user/findAll";

        String forObject = restTemplate.getForObject(url, String.class);
        return forObject;
    }



    public String fallback(){
        return "服务器繁忙，请稍后重试！";
    }


}
